package com.regentsoft.cloudback.operateAdmin.service.impl;

import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import com.regentsoft.cloudback.base.Pager;
import com.regentsoft.cloudback.operateAdmin.dao.ActivationCodeMapper;
import com.regentsoft.cloudback.operateAdmin.dao.ActivationMapper;
import com.regentsoft.cloudback.operateAdmin.dao.PdaMachineCodeMapper;
import com.regentsoft.cloudback.operateAdmin.entity.ActivationCode;
import com.regentsoft.cloudback.operateAdmin.entity.PdaMachineCode;
import com.regentsoft.cloudback.operateAdmin.service.PdaMachineCodeService;

/**
 * 
 * @author czd
 *
 * @date 2017年11月25日
 *
 * @version 1.0
 */
@Service
public class PdaMachineCodeServiceImpl  implements PdaMachineCodeService {
	@Autowired
	private PdaMachineCodeMapper mapper;
	@Autowired
	private ActivationMapper activationMapper;
	
	@Autowired
	private ActivationCodeMapper activationCodeMapper;
	@Override
	public PdaMachineCode findById(String pk) throws Exception {
		return mapper.findById(pk);
	}

	@Override
	public List<PdaMachineCode> findList(PdaMachineCode obj) throws Exception {
		return mapper.findList(obj);
	}

	@Override
	public Pager findPageList(PdaMachineCode obj, Integer pageNumber, Integer pageSize) throws Exception {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public int add(PdaMachineCode obj) throws Exception {
		return mapper.add(obj);
	}

	@Override
	public int update(PdaMachineCode obj) throws Exception {
		return mapper.update(obj);
	}

	@Override
	public int delete(String pk) throws Exception {
		return mapper.deleteById(pk);
	}

	@Override
	public int getTotalCount(Map<String, Object> map) throws Exception {
		return mapper.getTotalCount(map);
	}

	@Override
	public List<PdaMachineCode> queryByPage(Map<String, Object> map) throws Exception {
		// TODO Auto-generated method stub
		return null;
	}
	/**
	 * 0.绑定成功
	 * 1.激活码不存在
	 * 2.绑定失败
	 * 3.激活码已用完
	 */
	@Override
	@Transactional
	public int bandPdaMachine(String activationCode,String code) throws Exception {
		try {
			String companyId = activationMapper.getCompanyId(activationCode);
			if(StringUtils.isEmpty(companyId)) {
				return 1;
			}
			//判断是否已经绑定了
			int i = mapper.selectByCompanyId(code,companyId);
			if(i>0) {
				return 4;
			}
			int num  = activationCodeMapper.findByCode(activationCode);
			if(num==0) {
				return 3;
			}
			// 检验是否有可用的activationCode，并消费activationCode
			ActivationCode ac  = activationCodeMapper.findUseCode(activationCode);
			if(ac!=null) {
				//生成机器码记录
				PdaMachineCode pda = new PdaMachineCode();
				pda.setId(UUID.randomUUID().toString());
				
				ac.setStatus(1);
				ac.setPdaCode(code);
				ac.setActiveTime(new Date());
				ac.setPdaId(pda.getId());
				i = activationCodeMapper.updateByCode(ac);
				
				if(i>0) {
					pda.setCode(code);
					pda.setCompanyId(companyId);
					pda.setStatus(true);
					mapper.add(pda);
				}else {
					return 3;
				}
				
				
				
			}else {
				return 3;
			}
		} catch (Exception e) {
			throw new RuntimeException(e.getMessage(),e);
		}
		return 0;
	}

	@Override
	public boolean selectByCode(String code) {
		return mapper.selectByCode(code)>0;
	}

}
